package com.zhangtai.modules.service.impl.common;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dao.NoticeDao;
import com.zhangtai.modules.entity.common.NoticeEntity;
import com.zhangtai.modules.service.common.NoticeService;
import com.zhangtai.utils.DateUtils;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangtai.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;


@RestController
public class NoticeServiceImpl extends ServiceImpl<NoticeDao, NoticeEntity> implements NoticeService {

    @Override
    public PageResult queryPage(
                                @RequestParam(value = "type", required = false) Integer type,
                                @RequestParam(value = "msgType", required = false) Integer msgType,
                                @RequestParam(value = "noticeType", required = false) Integer noticeType,
                                @RequestParam(value = "accepterId", required = false) String accepterCode,
                                @RequestParam(value = "title", required = false) String title,
                                @RequestParam(value = "isRead", required = false) Integer isRead,
                                @RequestParam(value = "isDone", required = false) Integer isDone,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        //推送待办
        Integer i=0;
        //消息
        Integer j=2;
        IPage<NoticeEntity> page = this.page(
                new Page<NoticeEntity>(pageNum,pageSize),
                new QueryWrapper<NoticeEntity>()
                        .eq(i.equals(type),"type",null)
                        .eq(j.equals(type),"notice_type",null)
                        .eq(msgType!=null,"type",msgType)
                        .eq("is_delete",0)
                        .eq(noticeType!=null,"notice_type",noticeType)
                         .like(!StringUtils.isEmpty(accepterCode),"accepter_code",accepterCode).
                        like(!StringUtils.isEmpty(title),"title",title).eq(!ObjectUtils.isEmpty(isDone),"is_done",isDone).
                        eq(!ObjectUtils.isEmpty(isRead),"is_read",isRead).orderByDesc("create_time")
        );

        return new PageResult(page);
    }

    @Override
    public List<NoticeEntity> getNoticeByMonth( @RequestParam(value = "accepterId", required = false) String accepterCode,
                                                @RequestParam(value = "dateStr", required = false) String dateStr){
        String monthBegin;
        String monthEnd ;
        if(!StringUtils.isEmpty(dateStr)){
            monthBegin = dateStr.substring(0,7)+"-01 00:00:00";
            monthEnd =  dateStr.substring(0,7)+"-31 23:59:59";
        }else{
            monthBegin = DateUtils.getBeginDayOfMonth()+" 00:00:00";
            monthEnd = DateUtils.getLastDayOfMonth()+" 23:59:59";
        }
        return this.list(new QueryWrapper<NoticeEntity>().eq("is_done",0).between("todo_date",monthBegin,monthEnd).
                like(!StringUtils.isEmpty(accepterCode),"accepter_code",accepterCode).orderByDesc("todo_date"));
    }

    @Override
    public List<NoticeEntity> getNoticeToDoByRelateId(Long relateId,Integer type){
        return  this.list(new QueryWrapper<NoticeEntity>().eq("type",type).eq("operation_id",relateId).eq("is_delete",0).eq("is_done",0));
    }

    @Override
    public NoticeEntity getByIdE(Long id) {
            NoticeEntity one = this.getOne(new QueryWrapper<NoticeEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(NoticeEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(NoticeEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            NoticeEntity entity = new NoticeEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}